<?php
/* ================================================================================
 * Menu Class 
 *
 * menu.class.php: This class is used to drive the menu system.
 *
 * Author: Eric Hendrickson (enhendrickson@liberty.edu)
 * Date: 2/28/2006
   ================================================================================ */
class Menu
{
	function createMenu($id)
	{
		$MENU_HTML = '';

		global $DB,$GlobalSettings;
		$DB->query("SELECT * FROM menu WHERE MinistryID = '" . $id . "' ORDER BY LinkOrder");
		//if it didn't find something then see if it is an application 
		if($DB->get_num_rows() <= 0)
		{
			$DB->query("SELECT * FROM menu WHERE AppID = '" . $id . "' ORDER BY LinkOrder");
		}
		
		$MENU_ITEMS = '';
		while($row = $DB->fetch_row())
		{
			$MENU_ITEMS = $MENU_ITEMS . '<a href="' . $row['Link'] . '">' . $row['Display'] . '</a><br>';
		}
		
		if($MENU_ITEMS == '')
		{
			return '';
		}
		else
		{
			$MENU_HTML = $MENU_HTML . $MENU_ITEMS . '';
			return $MENU_HTML;
		}
	}
	
	function createMenuTitle($id)
	{
		global $DB,$GlobalSettings;
		$DB->query("SELECT MinistryName FROM " . 	$GlobalSettings['DivisionsTable'] . " WHERE " . $GlobalSettings['DivisionElement'] . " = '" . $id . "'");
		$row = $DB->fetch_row();
		
		$MENU_HTML = '
		<table cellpadding="0" cellspacing="0" style="border-bottom:3px dotted #cd9d1d;" >
			<tr>
				<td style="width:150px;"><span style="color:#000000;" class="TB1">' . $row['MinistryName'] .'</span></td>
			</tr>
		</table>';
		return $MENU_HTML;
	}
	
	function moveMenuItem($id, $type, $order, $direction)
	{
		if($direction == 'U')
		{
			$desiredOrder = $order - 1;
			if($desiredOrder <= 0)
				return;
		}
		elseif($direction == 'D')
		{
			$desiredOrder = $order + 1;
			//grab the max link
			$maxLink = $this->getMaxLinkOrder($id,$type);
			
			if($desiredOrder > $maxLink)
				return;
		}
		else
		{
			print 'Bad Interface';
			exit();
		}

		global $DB;
		//remove the primary key
		$DB->query("ALTER TABLE menu DROP PRIMARY KEY");
		$db_string = $DB->compile_db_update_string( 
			array (
				'LinkOrder' =>  $this->createLinkSpace($desiredOrder,$type,$id,$direction),
				$this->getSQLCol($type) => $id
				)
			);
		
		$DB->query("UPDATE menu SET " . $db_string . " WHERE " . $this->getSQLCol($type) . " = '" . $id . "' AND LinkOrder = '" . $order . "'" );

		$DB->query("UPDATE menu SET LinkOrder = " . $order . " WHERE " . $this->getSQLCol($type) . " = '" . $id . "' AND LinkOrder = 9999999");
		//add the primary key back
		$DB->query("ALTER TABLE menu ADD PRIMARY KEY (AppID,MinistryID,LinkOrder)");
	}

	function insertMenuItem($display,$link,$type,$id)
	{
		global $DB;
		
		//let the DB class handle the replacement
		$display = ereg_replace("\\\'","'",$display);

		$db_string = $DB->compile_db_insert_string( 
			array (
				'Display'	=> $display,
				'Link' => $link,
				'LinkOrder' =>  $this->getMaxLinkOrder($id, $type)+1,
				$this->getSQLCol($type) => $id
				)
			);
		
		$DB->query("INSERT INTO menu (".$db_string['FIELD_NAMES'].") VALUES (".$db_string['FIELD_VALUES'].")");
	}
	
	function updateMenuItem($display,$link,$order,$originalOrder,$type,$id)
	{
		//this code commented out due to below comment 
		/*if($originalOrder > $order) {
			$direction = 'up';
		}
		elseif($originalOrder < $order) {
			$direction = 'down';
		}
		else {
			$direction = 'none';
		}*/
			
		//let the DB class handle the replacement
		$display = ereg_replace("\\\'","'",$display);

		global $DB;
		$db_string = $DB->compile_db_update_string( 
			array (
				'Display'	=> $display,
				'Link' => $link,
		//this does not work quite right!? I decided to go a different route entirely...I couldn't fool-proof this method, so...I changed the interface (it was convoluted anyway :))
		//I replaced this functionality with the 'Up|Down' links and the moveMenuItem function in this class
		// Eric Hendrickson -- 06/06/06
	  //'LinkOrder' =>  $this->createLinkSpace($order,$type,$id,$direction),
				$this->getSQLCol($type) => $id
				)
			);
		
		$DB->query("UPDATE menu SET " . $db_string . " WHERE " . $this->getSQLCol($type) . " = '" . $id . "' AND LinkOrder = '" . $originalOrder . "'" );
	}

	function createLinkSpace($order,$type,$id,$direction)
	{
		//if it's negative or blank then hop out 
		if($order <= 0 || trim($order) == "")
		{
			return $order;
		}
		
		//get the column name by type 
		$SQLColType = $this->getSQLCol($type);
		
		//grab the max link
		$maxLink = $this->getMaxLinkOrder($id,$type);
	
		//if the desired order is greater than the max link then hop out
		if($order > $maxLink)
		{
			return $order;
		}
			
		global $DB;
		
		//this does not work quite right!? I decided to go a different route entirely...I couldn't fool-proof this method, so...I changed the interface (it was convoluted anyway :))
		//I replaced this functionality with the 'Up|Down' links and the moveMenuItem function in this class
		// Eric Hendrickson -- 06/06/06
		/*

		//figure out if this one has already been used
		$DB->query("SELECT * FROM menu WHERE " . $SQLColType . " = '" . $id . "' AND LinkOrder = '".$order."'");
		$used = $DB->get_num_rows();

		//if it hasn't already been used and...
		if(!$used)
		{
			//it is one above the max return it or it is below the max 
			if($order - $maxLink == 1 || $order < $maxLink)
			{
				return $order;
			}
			//it is way above the max return max + 1
			if($order - $maxLink > 1)
			{
				return $maxLink + 1;
			}
		} 
		else
		{	*/
		
		//swap up or down 
		if($direction == 'U') 
		{
			$DB->query("UPDATE menu SET LinkOrder = 9999999 WHERE " . $SQLColType . " = '" . $id . "' AND LinkOrder = " . $order );
		}
		elseif($direction == 'D') 
		{
			$DB->query("UPDATE menu SET LinkOrder = 9999999 WHERE " . $SQLColType . " = '" . $id . "' AND LinkOrder = " . $order );
		}
		else
		{
			print 'There was an error in the menu.class.php file.  Please contact your administrator.';
			exit();
		}
		
		return $order;
		//}
	}	
	
	function getSQLCol($type) 
	{
		//type D = Division
		//type A = Application
		
		global $GlobalSettings;
		if($type == 'D')
		{
			return $GlobalSettings['DivisionElement'];
		}
		else
		{
			if($type == 'A')
			{
				return 'AppID';
			}
			else
			{
				print 'There was an error in the menu.class.php file.  Please contact your administrator.';
				exit();
			}
		}
	}
	
	function getMaxLinkOrder($id,$type) 
	{
		global $DB; 
		$DB->query("select max(LinkOrder) as MAX_NUM from menu where " . $this->getSQLCol($type) . " = '" . $id. "'");
		$row = $DB->fetch_row();
		return $row['MAX_NUM']; 
	}
}
?>
